﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeekShop.Commands;

namespace GeekShop.Tools
{
    public class ToolManager
    {
        #region Properties
        //work-around to prevent the click event to be invoked twice
        private bool firstClick = false;
        private List<ITool> tools = new List<ITool>();
        private ITool activeTool;
        private PictureManager pictureManager;
        #endregion

        #region Member
        public List<ITool> Tools
        {
            get { return this.tools; }
        }
        public ITool ActiveTool
        {
            get { return this.activeTool; }
            set { this.activeTool = value; }
        }
        #endregion

        #region Constructor
        public ToolManager(PictureManager pictureManager)
        {
            this.pictureManager = pictureManager;

            this.tools.Add(new Tools.Select.Rectangle());
            this.tools.Add(new Tools.ColorPicker.SolidColor());
            this.tools.Add(new Tools.ColorPicker.Gradient());
            this.tools.Add(new Tools.Fill.SolidColor());
            this.tools.Add(new Tools.Fill.Gradient());
            this.tools.Add(new Tools.Transform.Move());

            foreach (ITool tool in this.tools)
            {
                tool.ToolReady += new EventHandler(ToolReady);
            }
        }
        #endregion

        #region Public Methods
        public void activateTool(object sender, ToolEventArgs e)
        {
            //work-around to prevent the click event to be invoked twice
            if (this.firstClick && this.pictureManager.SelectedPicture != null)
            {
                PictureBox box = this.pictureManager.SelectedPicture.Form.PictureBox;
                e.ActiveTool.RunDialog(this.pictureManager);
 
                #region Unregister old listeners
                if (this.activeTool != null)
                {
                    MouseBehaviour oldBehaviour = (MouseBehaviour)this.activeTool;
                    box.MouseDown -= new MouseEventHandler(oldBehaviour.MouseDown);
                    box.MouseMove -= new MouseEventHandler(oldBehaviour.MouseMove);
                    box.MouseUp -= new MouseEventHandler(oldBehaviour.MouseUp);
                }
                #endregion

                #region Register new listeners
                this.activeTool = e.ActiveTool;
                MouseBehaviour newBehaviour = (MouseBehaviour)e.ActiveTool;
                box.MouseDown += new MouseEventHandler(newBehaviour.MouseDown);
                box.MouseMove += new MouseEventHandler(newBehaviour.MouseMove);
                box.MouseUp += new MouseEventHandler(newBehaviour.MouseUp);
                #endregion
            }
            this.firstClick = !this.firstClick;
        }
        #endregion

        #region Event Methods
        private void ToolReady(object sender, EventArgs e)
        {
            try
            {
                this.pictureManager.SelectedPicture.DoCommand(((ITool)sender).Command);
                this.pictureManager.SelectedPicture.Form.Refresh();
            }
            catch (NullReferenceException)
            { }

        }
        #endregion
    }
}
